package com.lili.math;

/**
 * @Auther: 李 力
 * @Date: 2024/8/2
 * @Description: 交替位二进制数
 * @version: 1.0
 */
public class LeetCode693 {
    /*
     *  时间复杂度: O(n)
     *  空间复杂度: O(1)
     * */
    public boolean hasAlternatingBits(int n) {
        String binaryString = Integer.toBinaryString(n);
        for (int i = 0; i < binaryString.length() - 1; i++) {
            char c = binaryString.charAt(i);
            char b = binaryString.charAt(i + 1);
            if (b == c) {
                return false;
            }
        }
        return true;
    }

    /*
     * 时间复杂度: O(logn)
     * 空间复杂度: O(1)
     */
    public boolean hasAlternatingBits2(int n) {
        int prev = 2;
        while (n != 0) {
            int curr = n % 2;
            if (curr == prev) {
                return false;
            }
            prev = curr;
            n = n / 2;
        }
        return true;
    }

}
